package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsArea;
import com.yidu.ruifeng_logistics.entity.LogisticsPrice;
import com.yidu.ruifeng_logistics.entity.LogisticsStorage;
import com.yidu.ruifeng_logistics.service.LogisticsAreaService;
import com.yidu.ruifeng_logistics.service.LogisticsPriceService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 价格表(LogisticsPrice)表控制层
 *
 * @author 周有
 * @since 2021-03-11 15:16:44
 */
@RestController
@RequestMapping("logisticsPrice")
public class LogisticsPriceController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsPriceService logisticsPriceService;

    /**
     * 服务对象
     */
    @Resource
    private LogisticsAreaService logisticsAreaService;

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("showUpdate")
    public BaseMessage selectOne(Integer id) {
        return BaseMessage.success(this.logisticsPriceService.queryById(id));
    }

    /**
     * 修改、增加
     * @param logisticsPrice
     * @return
     * StringUtils
     */
    @ApiOperation(value = "根据id判断是否增加或修改价格表")
    @PostMapping ("addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsPrice logisticsPrice){
        if (StringUtils.isEmpty(logisticsPrice.getPriceId())){
            logisticsPriceService.insert(logisticsPrice);
        }else {
            logisticsPriceService.update(logisticsPrice);
        }
        return BaseMessage.success();
    }

    /**
     * 全查
     * @param price
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询全部价格")
    @GetMapping("findAll")
    public Map<String,Object> findAll(LogisticsPrice price, int page, int limit){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);

        Map<String,Object> map = new HashMap<>();
        List<LogisticsPrice> list = logisticsPriceService.queryAll(price);
        //查询到集合之后，将集合数据封装到分页助手pageInfo对象中
        PageInfo<LogisticsPrice> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);//这里可以直接写list，也可以写pageInfo.getList()
        map.put("count",pageInfo.getTotal());//总行数自动生成
        return map ;

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping("/delete")
    public BaseMessage delete(Integer id){
        return logisticsPriceService.deleteById(id)?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping("/deleteAll")
    public BaseMessage deleteAll(String ids){
        return logisticsPriceService.deleteAll(ids)>0?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * @param logisticsPrice
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAll")
    public  Map<String, Object> queryAll(LogisticsPrice logisticsPrice, int page, int limit,String area){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);
        //给logisticsPrice设置area属性
        logisticsPrice.setArea(area);
        Map<String, Object> map = new HashMap<>();
        List<LogisticsPrice>list = logisticsPriceService.queryAll(logisticsPrice);
        //查询到集合之后，将集合数据封装到分页助手pageInfo对象中
        PageInfo<LogisticsPrice>pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",pageInfo.getList());//可以直接写list
        map.put("count",pageInfo.getTotal());//总行数
        return map;
    }


    /**
     * @success
     * @return
     * 下拉框
     */
    @PostMapping("/createOption")
    public BaseMessage createOption(){
        return BaseMessage.success(logisticsAreaService.createOption());
    }
}
